package com.inossem.warehousestorage_inossem.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.format.Time;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.UtilsConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.bean.common.KeyValueBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryAreaBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryOrdinaryMatReqBean;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryOrdinaryMaterieBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/3 0003.
 */

public class InventoryUtils {
    //明盘
    public static final String VOUCHER_TYPE_1 = "1";

    public static void createEditText(Context context, ViewGroup viewGroup, String name, boolean required, String text, int id) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.view_inventory_edittext, null);
        view.setId(id);
        TextView nameTextView = (TextView) view.findViewById(R.id.name);
        EditText textEditText = (EditText) view.findViewById(R.id.text);
        if (required) {
            String nameStr = name + "*";
            SpannableStringBuilder builder = new SpannableStringBuilder(nameStr);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
            builder.setSpan(redSpan, nameStr.length() - 1, nameStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            nameTextView.setText(builder);
        } else {
            nameTextView.setText(name);
        }
        if (!TextUtils.isEmpty(text)) {
            textEditText.setText(text);

        }
        viewGroup.addView(view);
    }

    public static void createDatePicker(BaseActivity activity, ViewGroup viewGroup, String name, boolean required, String text, int id) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.view_inventory_textview, null);
        view.setId(id);
        TextView nameTextView = (TextView) view.findViewById(R.id.name);
        TextView textTextView = (TextView) view.findViewById(R.id.text);
        if (required) {
            String nameStr = name + "*";
            SpannableStringBuilder builder = new SpannableStringBuilder(nameStr);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
            builder.setSpan(redSpan, nameStr.length() - 1, nameStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            nameTextView.setText(builder);
        } else {
            nameTextView.setText(name);
        }
        if (!TextUtils.isEmpty(text)) {
            textTextView.setText(text);
        }
        textTextView.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                getTimePickerDialog(activity, textTextView.getText().toString(), (TimePickerDialog timePickerView, long millseconds) -> {
                    textTextView.setText(Utils.getStringDateFromTimeStamp(UtilsConstant.YYYY_MM_DD, millseconds));
                }).show(activity.getSupportFragmentManager(), "datePicker");
            }
        });
        viewGroup.addView(view);
    }

    private static TimePickerDialog getTimePickerDialog(Context context, String time, OnDateSetListener onDateSetListener) {
        TimePickerDialog.Builder yearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setThemeColor(ContextCompat.getColor(context, R.color.include_header_background))
                .setCallBack(onDateSetListener)
                .setTitleStringId(context.getResources().getString(R.string.common_select_date));
        if (time == null || "".equals(time)) {
            yearMonthDay.setCurrentMillseconds(System.currentTimeMillis());
        } else {
            yearMonthDay.setCurrentMillseconds(Utils.getTimeStampFromString(UtilsConstant.YYYY_MM_DD, time));
        }
        return yearMonthDay.build();
    }

    public static void createSelect(Context context, ViewGroup viewGroup, String name, boolean required, String value, List<KeyValueBean.ListBean> selectList, int id) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.view_inventory_textview, null);
        view.setId(id);
        TextView nameTextView = (TextView) view.findViewById(R.id.name);
        TextView textTextView = (TextView) view.findViewById(R.id.text);
        TextView valueTextView = (TextView) view.findViewById(R.id.value);
        if (required) {
            String nameStr = name + "*";
            SpannableStringBuilder builder = new SpannableStringBuilder(nameStr);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
            builder.setSpan(redSpan, nameStr.length() - 1, nameStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            nameTextView.setText(builder);
        } else {
            nameTextView.setText(name);
        }
        textTextView.setText(getTextByValue(value, selectList));
        if (!TextUtils.isEmpty(value)) {
            valueTextView.setText(value);
        }
        textTextView.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {

                DialogUtils.getSelectDialog(context, selectList, new DialogUtils.SelectListener() {
                    @Override
                    public void onSelect(Dialog dialog, String text, String value) {
                        textTextView.setText(text);
                        valueTextView.setText(value);
                    }
                }).show();
            }
        });
        viewGroup.addView(view);
    }

    private static String getTextByValue(String value, List<KeyValueBean.ListBean> selectList) {
        for (int i = 0; i < selectList.size(); i++) {
            KeyValueBean.ListBean selectBean = selectList.get(i);
            if (selectBean.getCode().equals(value)) {
                return selectBean.getValue();
            }
        }
        return "";
    }

    /**
     * 判断是否为明盘
     *
     * @return
     */
    public static boolean isWhite(InventoryAreaBean areaBean) {
        return VOUCHER_TYPE_1.equals(areaBean.getVoucherType());
    }

    /**
     * 获取应盘点行
     *
     * @return
     */
    public static int getTotalRowCount(InventoryAreaBean bean) {
        int ret = 0;
        for (InventoryAreaBean.AreaListBean a :
                bean.getBody()) {
            ret += Integer.parseInt(a.getTotal());
        }
        return ret;
    }

    public static int getTotalCheckCount(InventoryAreaBean bean) {
        int ret = 0;
        for (InventoryAreaBean.AreaListBean a :
                bean.getBody()) {
            ret += Integer.parseInt(a.getCurrent());
        }
        return ret;
    }

    public static InventoryOrdinaryMatReqBean genUpdateItems(InventoryOrdinaryMaterieBean.DataBean bean) {
        InventoryOrdinaryMatReqBean r = new InventoryOrdinaryMatReqBean();
        List<InventoryOrdinaryMatReqBean.ItemListBean> list = new ArrayList<>();
        for (InventoryOrdinaryMaterieBean.DataBean.ItemListBean b : bean.getItem_list()) {
            if (!TextUtils.isEmpty(b.getNumber())) {
                InventoryOrdinaryMatReqBean.ItemListBean lb = new InventoryOrdinaryMatReqBean.ItemListBean();
                lb.setItem_id(b.getItem_id());
                lb.setStocktake_qty(b.getStocktake_qty());
                list.add(lb);
//                r.getItem_list().add(lb);
            }
        }
        r.setItem_list(list);
        return r;
    }

    public static String getTime() {
        Time t = new Time();
        t.setToNow();
        return t.year + "-" + t.month + "-" + t.monthDay;
    }
}
